import { toJS } from 'mobx';
import { observable, action } from 'mobx';
import BaseListStore from '../BaseListStore';
import * as Apis from '../../apis/ReportApi';
import { message, Button } from 'antd';

class ReportStore extends BaseListStore {
  constructor(props) {
    super(props);
    this.searchInputName = 'name';
  }
    @observable workplanDialog = { workPlanId: 0, visible: false, activeKey: '1' };
    @action
    showWorkPlanDialog = (bool = false, randomFieldId) => {
      this.workplanDialog.visible = bool;
      this.workplanDialog.workPlanId = randomFieldId;
    }

    getTableData(requestParams, callBackFn) {
      Apis.getContactLogReport(requestParams).then((data) => {
        callBackFn(data);
      });
    }

    @action
    close = () => {
      this.workplanDialog.visible = false;
    }

    @action
    onlyClose = () => {
      this.workplanDialog.visible = false;
    }

    @action
    changeTab = (e) => {
      this.workplanDialog.activeKey = e;
    }

    setContactStatus = (randomFieldId) => {
      Apis.doFinishWorkPlan({ planIds: randomFieldId }).then((data) => {
        if (data.status == 'success') {
          window._contactLogTable.reLoad();
        } else {
          message.error(`请求失败：${data.msgcode}`);
        }
      });
    }

    getTopButtons() {
      const { rightMenu, tableStore } = this;
      const { selectedRowKeys } = tableStore;
      const btnArr = [];
      const isDisabled = !(selectedRowKeys.length > 0 && `${toJS(selectedRowKeys)}`);
      rightMenu && rightMenu.length > 0 && toJS(rightMenu).map((m) => {
        m.isTop == '1' && btnArr.length < 4 && btnArr.push(
          <Button
            ecId={`${this && this.props && this.props.ecId || ''}_Button@n8k2f1@${m.type}`}
            type="primary"
            disabled={isDisabled && m.isControl == '1'}
            onClick={() => {
              if (m.type == 'BTN_FINISH') { // 完成
                this.setContactStatus(`${selectedRowKeys}`);
              }
            }}
          >
            {m.menuName}
          </Button>,
        );
      });
      return btnArr;
    }

    rightMenuClick(key) {
      const { rightMenu, tableStore } = this;
      const { selectedRowKeys } = tableStore;
      if (key == 'BTN_FINISH') { // 完成
        this.setContactStatus(`${selectedRowKeys}`);
      }
    }
}

export default ReportStore;
